import { Component, ComponentType } from "react";

interface CompState {
  component: null | ComponentType
}

function AsyncComponent(compFactory:Function):ComponentType {
  
  class AsyncComponentGenerator extends Component {
    constructor(props:any) {
      super(props);
    }

    state:CompState = {
      component: null,
    }
    async componentDidMount() {
      let {default: component} = await compFactory();
      this.setState({component})
    }

    render() {
      let Comp = this.state.component;
      return Comp ? <Comp {...this.props} /> : null;
    }
  }
  return AsyncComponentGenerator;
}

export default AsyncComponent;